package com.bluetron.supos.apps.controllers;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bluetron.supos.apps.exceptions.AppError;
import com.bluetron.supos.apps.exceptions.AppException;
import com.bluetron.supos.apps.services.ExampleService;

/**
 * 示例控制器
 */
@RestController
public class ExampleController {

    @Autowired
    private ExampleService exampleService;

    /**
     * 打招呼
     * <p>
     * 参数校验，请尽量使用Hibernate Validator的注解来完成参数校验
     * 
     * @param name 姓名
     * @return
     */
    @GetMapping("/hello")
    public String sayHello(
            @Validated @Valid
            @RequestParam(name = "name", required = false) 
            @NotEmpty(message = "姓名不能为空") String name) {
        return this.exampleService.sayHello(name);
    }
    
    /**
     * 测试异常
     */
    @GetMapping("/error")
    public void doError() {
        throw new AppException(AppError.ERROR_TEST);
    }
}
